import axios from 'axios';
import qs from 'query-string';
import type { HttpResponse } from './interceptor';

export interface AddressId {
    id: number;
}

export interface AddressRecord {
    id: number;
    user_id: number;
    name: string;
    phone: string;
    province: string;
    city: string;
    district: string;
    detail: string;
    is_default: number;
    is_default_text: string;
    created_at: string;
    updated_at: string;
}

export interface AddressListRes {
    list: AddressRecord[];
    total: number;
}

export interface AddressModel {
    id?: number;
    user_id: number;
    name: string;
    phone: string;
    province: string;
    city: string;
    district: string;
    detail: string;
    is_default: number;
}

export interface AddressParams {
    current: number;
    pageSize: number;
    phone?: string;
    user_id?: number;
}

/**
 * 获取地址列表
 * @param params
 */
export function queryAddressList(params: AddressParams): Promise<HttpResponse<AddressListRes>> {
    return axios.get('/admin/address/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取地址详情
 * @param params
 */
export function queryAddressOne(params: AddressId): Promise<HttpResponse<AddressRecord>> {
    return axios.get('/admin/address/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 删除地址
 * @param id
 */
export function delAddress(id: number): Promise<HttpResponse<null>> {
    return axios.post('/admin/address/delete', { id });
} 